//
//  HYFNewFeatureController.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/23.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

private let cellID:String = "cell"

class HYFNewFeatureController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    func setupUI(){
        
        //添加控件
        view.addSubview(collectionView)
        //设置约束
        collectionView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
        
        collectionView.registerClass(HYFNewfeatureViewCell.self, forCellWithReuseIdentifier: cellID)
    }
 
    //懒加载
    private lazy var collectionView:UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: SCREENW, height: SCREENH)
        layout.scrollDirection = .Horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        view.backgroundColor = UIColor.greenColor()
        
        view.dataSource = self
        view.delegate = self
        
        view.pagingEnabled = true
        view.bounces = false
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        
        return view
    }()

}

extension HYFNewFeatureController:UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 4
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as! HYFNewfeatureViewCell
        
        cell.backgroundColor = RandomColor()
        
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    //滚动到最后一页的时候,才显示 btn
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        //计算偏移量的时候,要用 scrollView 来计算
        let index = Int(scrollView.contentOffset.x / SCREENW)
        if  index == 3 {
         
            //取出 cell, 要强转类型(系统默认是 UICollectionViewCell) 才能 使用 自定义 cell 的方法
            let cell = self.collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! HYFNewfeatureViewCell
            // 开始动画
            cell.startAnim()
        }
    }
    
}

//自定义 cell
class HYFNewfeatureViewCell:UICollectionViewCell{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置一个属性供外界赋值
    var imageIndex:Int = 0{
        
        didSet{
         bgImageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
         
         startButton.hidden = true
        }
    }
    
    func setupUI(){
        
        //添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(startButton)
        //设置约束
        bgImageView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        startButton.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-100)
        }
    }
    
    //懒加载控件
    private lazy var bgImageView:UIImageView = UIImageView()
    
    private lazy var startButton:UIButton = {
        
       let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named:"new_feature_finish_button"), forState: UIControlState.Normal)
        //设置title
        btn.setTitle("欢迎光临", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        
        //添加监听
        btn.addTarget(self, action: #selector(HYFNewfeatureViewCell.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.sizeToFit()
        
        return btn
    }()
    
    func buttonClick(){
        
        //发送通知 --> 告知 appDelegate 切换根控制器
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: "newFeature")
        
    }
    
    //最后一个 item 的时候,执行 label 的动画
    func startAnim(){
        
        startButton.hidden = false
        startButton.userInteractionEnabled = false
        //动画
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        //动画还原
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { 
            
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) in
                
                self.startButton.userInteractionEnabled = true
        }
    }
}







